/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdSceneMapStep;
import com.comac.ins.isfd.domain.bo.IsfdSceneMapStepBo;
import com.comac.ins.isfd.domain.bo.IsfdSceneMapStepModelRefBo;
import com.comac.ins.isfd.domain.vo.IsfdSceneMapStepModelRefVo;
import com.comac.ins.isfd.domain.vo.IsfdSceneMapStepVo;
import com.comac.ins.isfd.domain.vo.blood.BloodRelationshipOuterV2Vo;
import com.comac.ins.isfd.mapper.IsfdSceneMapStepMapper;
import com.comac.ins.isfd.service.IIsfdSceneMapStepModelRefService;
import com.comac.ins.isfd.service.IIsfdSceneMapStepService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 系统地图-步骤Service业务层处理
 *
 * @author hxloongs
 * @date 2025-03-03
 */
@RequiredArgsConstructor
@Service
public class IsfdSceneMapStepServiceImpl implements IIsfdSceneMapStepService {

    @Autowired
    private IsfdSceneMapStepMapper baseMapper;
    @Autowired
    private IIsfdSceneMapStepModelRefService iIsfdSceneMapStepModelRefService;

    @Override
    public IsfdSceneMapStepVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public TableDataInfo<IsfdSceneMapStepVo> queryPageList(IsfdSceneMapStepBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdSceneMapStep> lqw = buildQueryWrapper(bo);
        Page<IsfdSceneMapStepVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<IsfdSceneMapStepVo> queryList(IsfdSceneMapStepBo bo) {
        LambdaQueryWrapper<IsfdSceneMapStep> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdSceneMapStep> buildQueryWrapper(IsfdSceneMapStepBo bo) {
        LambdaQueryWrapper<IsfdSceneMapStep> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStepDescription()), IsfdSceneMapStep::getStepDescription, bo.getStepDescription());
        lqw.eq(bo.getStepRank() != null, IsfdSceneMapStep::getStepRank, bo.getStepRank());
        return lqw;
    }

    @Override
    public Boolean insertByBo(IsfdSceneMapStepBo bo) {
        IsfdSceneMapStep add = MapstructUtils.convert(bo, IsfdSceneMapStep.class);
        baseMapper.insert(add);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateByBo(IsfdSceneMapStepBo bo) {
        IsfdSceneMapStep update = MapstructUtils.convert(bo, IsfdSceneMapStep.class);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional
    public void deleteWithByMapId(Long mapId) {
        List<IsfdSceneMapStepVo> isfdSceneMapStepVos = baseMapper.selectVoList(new LambdaQueryWrapper<IsfdSceneMapStep>()
            .eq(IsfdSceneMapStep::getMapId, mapId));
        for (IsfdSceneMapStepVo mapStepVo : isfdSceneMapStepVos) {
            Long mapStepId = mapStepVo.getId();
            baseMapper.deleteById(mapStepId);
            iIsfdSceneMapStepModelRefService.deleteByMapStepId(mapStepId);
        }
    }

    @Override
    public List<IsfdSceneMapStepVo> queryListByMapId(Long mapId, Boolean needStatistics) {
        List<IsfdSceneMapStepVo> isfdSceneMapStepVos = baseMapper.selectVoList(new LambdaQueryWrapper<IsfdSceneMapStep>()
            .eq(IsfdSceneMapStep::getMapId, mapId)
            .orderByAsc(IsfdSceneMapStep::getStepRank));
        if (CollectionUtils.isEmpty(isfdSceneMapStepVos)) {
            return new ArrayList<>();
        }
        for (IsfdSceneMapStepVo vo : isfdSceneMapStepVos) {
            vo.setModelRefList(iIsfdSceneMapStepModelRefService.queryListByMapStepId(vo.getId(), needStatistics));
        }
        return isfdSceneMapStepVos;
    }

    @Override
    @Transactional
    public void save(Long mapId, List<IsfdSceneMapStepBo> isfdSceneMapStepList, Long newMapId) {
        if (mapId != null) {
            List<IsfdSceneMapStepVo> isfdSceneMapStepVos = baseMapper.selectVoList(new LambdaQueryWrapper<IsfdSceneMapStep>()
                .eq(IsfdSceneMapStep::getMapId, mapId));
            for (IsfdSceneMapStepVo isfdSceneMapStepVo : isfdSceneMapStepVos) {
                Long mapStepId = isfdSceneMapStepVo.getId();
                iIsfdSceneMapStepModelRefService.deleteByMapStepId(mapStepId);
            }
            this.deleteWithByMapId(mapId);
        }

        for (IsfdSceneMapStepBo mapStepBo : isfdSceneMapStepList) {
            IsfdSceneMapStep isfdSceneMapStep = MapstructUtils.convert(mapStepBo, IsfdSceneMapStep.class);
            isfdSceneMapStep.setMapId(mapId != null ? mapId : newMapId);
            baseMapper.insert(isfdSceneMapStep);
            Long mapStepId = isfdSceneMapStep.getId();
            for (IsfdSceneMapStepBo.ChoseDataBaseAndModel choseDataBaseAndModel : mapStepBo.getModelRefList()) {
                IsfdSceneMapStepModelRefBo mapStepModelRefBo = new IsfdSceneMapStepModelRefBo();
                mapStepModelRefBo.setMapStepId(mapStepId);
                mapStepModelRefBo.setModelId(choseDataBaseAndModel.getModelId());
                mapStepModelRefBo.setDatabaseTypeId(choseDataBaseAndModel.getDatabaseTypeId());
                iIsfdSceneMapStepModelRefService.insertByBo(mapStepModelRefBo);
            }
        }
    }

    @Override
    public IsfdSceneMapStepModelRefVo querySceneMapStepModelRefVo(Long sceneMapStepModelRefId) {
        IsfdSceneMapStepModelRefVo mapStepModelRefVo = iIsfdSceneMapStepModelRefService.queryById(sceneMapStepModelRefId);
        if (mapStepModelRefVo == null) {
            throw new BaseException("关联关系不存在，请确认后重试！");
        }
        return mapStepModelRefVo;
    }

    @Override
    public List<BloodRelationshipOuterV2Vo> relationship(Long sceneId, Long systemId, Long databaseTypeId, Long modelId) {
        return iIsfdSceneMapStepModelRefService.relationship(sceneId, systemId, databaseTypeId, modelId);
    }

    @Override
    public List<IsfdSceneMapStepVo> findMapStepVoListByDatabaseIdAndModelId(Long databaseTypeId, Long modelId) {
        IsfdSceneMapStepModelRefBo bo = new IsfdSceneMapStepModelRefBo();
        bo.setDatabaseTypeId(databaseTypeId);
        bo.setModelId(modelId);
        List<IsfdSceneMapStepModelRefVo> isfdSceneMapStepModelRefVoList = iIsfdSceneMapStepModelRefService.queryList(bo);
        if (CollectionUtils.isEmpty(isfdSceneMapStepModelRefVoList)) {
            return new ArrayList<>();
        }
        List<Long> mapStepIdList = isfdSceneMapStepModelRefVoList.stream().map(IsfdSceneMapStepModelRefVo::getMapStepId).toList();
        return baseMapper.selectVoList(new LambdaQueryWrapper<IsfdSceneMapStep>().in(IsfdSceneMapStep::getId, mapStepIdList));
    }
}
